/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-12-01     Administrator       the first version
 */
#include <board.h>
#include <rtthread.h>
#include "ir.h"
#include "pwm.h"



#ifdef BSP_USING_IRRX_HW
///硬件IR receiver参数
#define IR32KSEL_EN         0                     //IR clock source select 32K
#if IR32KSEL_EN
    #define RPTERR_CNT      33                    //配置11.25ms ± (RPTERR_CNT + 1)*32K的repeat code允许范围
    #define DATERR_CNT      33                    //配置13.5ms ± (DATERR_CNT + 1)*32K引导码允许范围
    #define ONEERR_CNT      7                     //配置2.25ms ± (ONEERR_CNT + 1)*32K的logic "1"允许范围
    #define ZEROERR_CNT     3                     //配置1.12ms ± (ONEERR_CNT + 1)*32K数logic "0"允许范围
    #define TOPR_CNT        55                    //IR time out length = (TOPR + 1) * 64 *32K
#else
    #define RPTERR_CNT      1000                  //配置11.25ms ± (RPTERR_CNT + 1)us的repeat code允许范围
    #define DATERR_CNT      1000                  //配置13.5ms ± (DATERR_CNT + 1)us引导码允许范围
    #define ONEERR_CNT      250                   //配置2.25ms ± (ONEERR_CNT + 1)us的logic "1"允许范围
    #define ZEROERR_CNT     125                   //配置1.12ms ± (ONEERR_CNT + 1)us数logic "0"允许范围
    #define TOPR_CNT        1718                  //IR time out length = (TOPR + 1) * 64 us
#endif // IR32KSEL_EN
#endif // BSP_USING_IRRX_HW

#define NO_KEY      (0u)

struct ab32_irrx_data{
    rt_uint16_t cnt;                            //ir data bit counter
    rt_uint16_t rpt_cnt;                        //ir repeat counter
    rt_uint16_t addr;                           //address,  inverted address   Extended NEC: 16bits address
    rt_uint16_t cmd;                            //command,  inverted command
};
typedef struct ab32_irrx_data *ab32_irrx_data_t;



//struct      //红外数据
//{
//    uint16_t power_key;
//    uint16_t up_key ;
//    uint16_t dower_key;
//    uint16_t rigt_key;
//    uint16_t left_key;
//    uint16_t enter_key;
//}ir_com_data;


static struct ab32_irrx_data _irrx = {0};


rt_flag_t power; //全局变量
rt_uint8_t add_gear_adj;
rt_uint8_t subb_gear_adj;
rt_uint8_t  pwm_data;

static void irrx_isr(int vector, void *param)
{
    rt_interrupt_enter();
    rt_kprintf("cmd=%hx addr=%hx\n",11,222);
    /* 如果有接收到有效的红外数据 */
    if (IRRXCON & BIT(16)) {
    /* 清除接收到数据的标志 */
        IRRXCPND = BIT(16);
        /* 从红外控制器对应的寄存器中读取接收到的数据 */
        _irrx.addr = (uint16_t)IRRXDAT;
        _irrx.cmd = (uint16_t)(IRRXDAT >> 16);
        /* 标记红外数据长度是 32 bit */
        _irrx.cnt = 32;
    }

    /* 如果检测到红外按键释放了,清零接收数据的长度 */
    if (IRRXCON & BIT(17)) {
        IRRXCPND = BIT(17);
        _irrx.cnt = 0;
    }

    rt_interrupt_leave();


}


 void _irrx_hw_init(void)
{
    /* 初始化红外管脚映射到 PE6 */
    GPIOEDE |= BIT(6);
    GPIOEPU |= BIT(6);
    GPIOEDIR |= BIT(6);
    FUNCMCON2 |= 0xf << 20;
    FUNCMCON2 |= (7 << 20);
    rt_memset(&_irrx, 0, sizeof(_irrx));

/* 根据 32K 或者 1M 不同的始终频率初始化配置 */
    IRRXERR0 = (RPTERR_CNT << 16) | DATERR_CNT;
    IRRXERR1 = (TOPR_CNT << 20) | (ONEERR_CNT << 10) | ZEROERR_CNT;

/* 如果选择了 32K 的时钟频率,需要额外配置时钟相关的寄存器以及开启红外对应寄存器的管脚 */
#if IR32KSEL_EN
    CLKCON1 &= ~BIT(5);
    CLKCON1 |= BIT(4);
    IRRXCON |= BIT(3);
#endif

/* 加载红外管脚中断处理函数 */
    rt_hw_interrupt_install(IRQ_IRRX_VECTOR, irrx_isr, RT_NULL, "irrx_isr");
 /* 使能红外以及红外中断 */
    IRRXCON = 0x03;

}

 void ir_receive( )
 {
#define   up_key         0x35ca;
#define   dower_key      0x2dd2;
#define   rigt_key       0x3ec1;
#define   left_key       0x6699;
#define   enter_key      0x31ce;

//     while (1)
//     {
         if (_irrx.addr != 0 || _irrx.cmd != 0 )
         {
             rt_kprintf("addr=%hx cmd=%hx\n", _irrx.addr, _irrx.cmd);

             if (_irrx.cmd== 0x23dc)
                     {
                        power=~power;
                        if(power)//power_on
                                   {
                                    pwm_vout_adj(35,1);//脉宽是%
                                   }
                        else
                                   {
                                       pwm_vout_adj(pwm_data,0);//脉宽是%
                                   }

                     }


                           if (_irrx.cmd==0x35ca)   //PWM加
                            {
                                if(power)
                                {
                                if(pwm_data>95)pwm_data=95;
                                pwm_vout_adj(pwm_data+=5,1);
                                }
                            }

                            if (_irrx.cmd== 0x2dd2)//PWM减
                            {
                                if(power)
                                {
                            if(pwm_data<35)pwm_data=35;
                            pwm_vout_adj(pwm_data-=5,1);
                                }
                            }



         }


         rt_memset(&_irrx, 0, sizeof(_irrx));//清零
//     }
 }
 //MSH_CMD_EXPORT(test_irda, test irda sample);
